﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;
using EHR.Common;

namespace CommunitySystem
{
    public partial class KaoGuanControl : UserControl
    {
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();

        private ObservableCollection<PublicOfficialSelectionService.KaoGuanModel> selectedempmodel =
            new ObservableCollection<PublicOfficialSelectionService.KaoGuanModel>();
        private PublicOfficialInfo poi = new PublicOfficialInfo();
        public KaoGuanControl()
        {
            InitializeComponent();
            //for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            //{
            //    ComboBoxItem cbi = new ComboBoxItem();
            //    cbi.Content = i.ToString();
            //    cbi.Tag = i;
            //    nian.Items.Add(cbi);
            //}
            //nian.SelectedIndex = 0;
            Load();
        }

        
        public void Load()
        {
            Client.GetKaoGuanModelCompleted += new EventHandler<GetKaoGuanModelCompletedEventArgs>(Client_GetKaoGuanModelCompleted);
            Client.GetKaoGuanModelAsync(name.Text, 0, Session.LoginUser.LoginName, "选调交流");
            Client.GetPublicOfficialInfoCompleted += new EventHandler<GetPublicOfficialInfoCompletedEventArgs>(Client_GetPublicOfficialInfoCompleted);
            Client.GetPublicOfficialInfoAsync(" Status = 1");
        }

        void Client_GetPublicOfficialInfoCompleted(object sender, GetPublicOfficialInfoCompletedEventArgs e)
        {

            if (e.Result != null&&e.Result.Count>0)

                poi = e.Result[0];
        }

        void Client_GetKaoGuanModelCompleted(object sender, GetKaoGuanModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            KaoGuanWindow RMWindow = new KaoGuanWindow(0, true);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as KaoGuanModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                KaoGuanWindow RMWindow = new KaoGuanWindow(Convert.ToInt32(seljw.ID), false);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的考官信息！");
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (KaoGuanModel i in (ObservableCollection<KaoGuanModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (KaoGuanModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    Client.DeteleKaoGuanModelCompleted -= new EventHandler<DeteleKaoGuanModelCompletedEventArgs>(Client_DeteleKaoGuanModelCompleted);
                    Client.DeteleKaoGuanModelCompleted += new EventHandler<DeteleKaoGuanModelCompletedEventArgs>(Client_DeteleKaoGuanModelCompleted);
                    Client.DeteleKaoGuanModelAsync("(" + ids.Substring(0, ids.Length - 1) + ")");
                }
            }
        }

        void Client_DeteleKaoGuanModelCompleted(object sender, DeteleKaoGuanModelCompletedEventArgs e)
        {
            Load();
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (KaoGuanModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }

        private int cou = 0;
        private void JZX_Click(object sender, RoutedEventArgs e)
        {
            if (poi != null && poi.Type != 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("没有正在发布的竞争性选拔工作！");
                return;
            }
            cou = 0;
            if (DGRole.ItemsSource != null)
            {
                foreach (KaoGuanModel i in (ObservableCollection<KaoGuanModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (KaoGuanModel i in selectedempmodel)
                {
                    KaoGuanDetailModel model1 = new KaoGuanDetailModel();
                    model1.KaoGuanID = i.ID;
                    model1.Year = poi.ID;
                    model1.XBType = "竞争性选拔";
                    Client.AddKaoGuanDetailModelCompleted -= new EventHandler<AddKaoGuanDetailModelCompletedEventArgs>(Client_AddKaoGuanDetailModelCompleted);
                    Client.AddKaoGuanDetailModelCompleted += new EventHandler<AddKaoGuanDetailModelCompletedEventArgs>(Client_AddKaoGuanDetailModelCompleted);
                    Client.AddKaoGuanDetailModelAsync(model1);
                    cou += 1;
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择信息！");
        }

        void Client_AddKaoGuanDetailModelCompleted(object sender, AddKaoGuanDetailModelCompletedEventArgs e)
        {
            if (e.Result == true && cou == selectedempmodel.Count)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("添加成功！");
                cou = 0;
            }
            if (e.Result == false && cou == selectedempmodel.Count)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该考官已经参加该选拔工作！");
                cou = 0;
            }
        }

        //void Client_AddKaoGuanDetailModelCompleted1(object sender, AddKaoGuanDetailModelCompletedEventArgs e)
        //{
        //    if (e.Result == true && cou == selectedempmodel.Count)
        //    {
        //        SilverlightMessageBoxLibrary.Message.InfoMessage("添加成功！");
        //    }
        //    if (e.Result == false) SilverlightMessageBoxLibrary.Message.ErrorMessage("该考官本年度已经参加选调交流工作！");
        //}

        private void XD_Click(object sender, RoutedEventArgs e)
        {
            if (poi != null && poi.Type != 1)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("没有正在发布的选调交流工作！");
                return;
            }
            cou = 0;
            if (DGRole.ItemsSource != null)
            {
                foreach (KaoGuanModel i in (ObservableCollection<KaoGuanModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (KaoGuanModel i in selectedempmodel)
                {
                    KaoGuanDetailModel model1 = new KaoGuanDetailModel();
                    model1.KaoGuanID = i.ID;
                    model1.Year = poi.ID;
                    model1.XBType = "选调交流";
                    Client.AddKaoGuanDetailModelCompleted -= new EventHandler<AddKaoGuanDetailModelCompletedEventArgs>(Client_AddKaoGuanDetailModelCompleted);
                    Client.AddKaoGuanDetailModelCompleted += new EventHandler<AddKaoGuanDetailModelCompletedEventArgs>(Client_AddKaoGuanDetailModelCompleted);
                    Client.AddKaoGuanDetailModelAsync(model1);
                    cou += 1;
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择信息！");
        }

        private void ZL_Click(object sender, RoutedEventArgs e)
        {
            cou = 0;
            if (DGRole.ItemsSource != null)
            {
                foreach (KaoGuanModel i in (ObservableCollection<KaoGuanModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (KaoGuanModel i in selectedempmodel)
                {
                    KaoGuanDetailModel model1 = new KaoGuanDetailModel();
                    model1.KaoGuanID = i.ID;
                    model1.Year = DateTime.Today.Year;
                    model1.XBType = "招录管理";
                    Client.AddKaoGuanDetailModelCompleted -= new EventHandler<AddKaoGuanDetailModelCompletedEventArgs>(Client_AddKaoGuanDetailModelCompleted);
                    Client.AddKaoGuanDetailModelCompleted += new EventHandler<AddKaoGuanDetailModelCompletedEventArgs>(Client_AddKaoGuanDetailModelCompleted);
                    Client.AddKaoGuanDetailModelAsync(model1);
                    cou += 1;
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择信息！");
        }
       
    }
}
